package com.mapeditor.core 
{
	import AS3Events.AS3EventMessage;
	import AS3Events.AS3EventRouter;
	import AS3Util.Tr;
	import com.mapeditor.graph.ShapeImage;
	import com.mapeditor.graph.ShapePolygon;
	import com.mapeditor.math.Vector3;
	import flash.events.Event;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	/**
	 * ...
	 * @author SE
	 */
	public class LoadXML
	{
		public var elements:Array;
		public var xmlData:XML;
		private var _loader:URLLoader;
		
		private static var instance:LoadXML;  
		
		public static function getInstance():LoadXML
		{
			if (instance == null)  
				instance = new LoadXML();  				
			return instance;  
		}
		function LoadXML() {
			_loader = new URLLoader();
			_loader.addEventListener(Event.COMPLETE, completeHandler);
			elements = new Array();
			
		}
		
		public function loadXML():void{
			var time:Date = new Date();
			var request:URLRequest  = new URLRequest("xml/source.xml?nocache="+time.getMilliseconds());
            try {				
				//Cria vidro de proteção 
				_loader.load(request);

            } catch (error:Error) {
                trace("Unable to load requested document.");
            }
			
		}
		
        private function completeHandler(event:Event):void {
            
			this.xmlData = new XML(URLLoader(event.target).data);
			
			elements = new Array();
			elements[0] = new Item("center", 0, new ShapeImage("img/3d.png"));
			var pos:Array = xmlData..coordinate.split(" ");			
			elements[0].x = pos[0];
			elements[0].y = pos[2];		
			
			
			_loadRoom();
			_loadPlatform();
			_loadTower();
			_loadShip();			
			_loadPortal();
			_loadPowerUp();
			
			AS3EventRouter.SendEvent("class_load", AS3EventMessage.CreateMessage("xml_loaded", "", null));
			
        }
		
		private function _loadPlatform():void
		{
			var size:Number = this.xmlData..platforms.Item.length();

			if (size <= 0)
				return;
			
			var i:uint = elements.length;
			for each (var platform:XML in xmlData..platforms.Item)
			{
				
				if(platform.name == "Platform")
					elements[i] = new Item(platform.name, Item.PLATFORM, new ShapeImage("img/terrain1.png",0,0,0.5));
				else if(platform.name == "BridgeVertical")
					elements[i] = new Item(platform.name, Item.BRIDGE, new ShapeImage("img/bridge.png",0,0,0.5));
				else
					elements[i] = new Item(platform.name, Item.BRIDGE, new ShapeImage("img/bridge_rotate.png",0,0,0.5));
				
				var pos:Array = platform.position.split(" ");			
				elements[i].x = _convertX(pos[0]);
				elements[i].y = _convertY(pos[2]);
				
				i++;
			}//end for
			
		}//end of method
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		private function _loadTower():void
		{
			var size:Number = this.xmlData..towers.Item.length();

			if (size <= 0)
				return;
			
			var i:uint = elements.length;
			for each (var item:XML in xmlData..towers.Item)
			{
				
				if(item.name =="Tower01")
					elements[i] = new Item(item.name, Item.TOWER, new ShapeImage("img/tower01.png"));
				else if(item.name=="Tower02")
					elements[i] = new Item(item.name, Item.TOWER, new ShapeImage("img/tower02.png"));
				else
					elements[i] = new Item(item.name, Item.TOWER, new ShapeImage("img/tower03.png"));
				
				var pos:Array = item.position.split(" ");			
				elements[i].x = _convertX(pos[0]);
				elements[i].y = _convertY(pos[2]);
				Item(elements[i]).life = item.life;
				Item(elements[i]).frequence = item.tiroFrequence;
				Item(elements[i]).dist = item.distTargetAtack; 
				
				i++;
			}//end for
		}//end of method
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		private function _loadShip():void
		{
			var size:Number = this.xmlData..ships.Item.length();

			if (size <= 0)
				return;
			
			var i:uint = elements.length;
			for each (var item:XML in xmlData..ships.Item)
			{
				//Tr.ace("name = " + item.name);
				
				if(item.name=="Ship01")
					elements[i] = new Item(item.name, Item.SHIP, new ShapeImage("img/ship01.png")); 
				else if(item.name=="Ship02")
					elements[i] = new Item(item.name, Item.SHIP, new ShapeImage("img/ship02.png"));
				else if(item.name=="Ship03")
					elements[i] = new Item(item.name, Item.SHIP, new ShapeImage("img/ship03.png"));
				else if(item.name=="Tank01")
					elements[i] = new Item(item.name, Item.SHIP, new ShapeImage("img/tank1.png"));
				else if(item.name=="Tank02")
					elements[i] = new Item(item.name, Item.SHIP, new ShapeImage("img/tank2.png"));
				else if(item.name=="Tank03")
					elements[i] = new Item(item.name, Item.SHIP, new ShapeImage("img/tank3.png"));
				else
					elements[i] = new Item(item.name, Item.SHIP, new ShapeImage("img/ship01.png")); 
					
				var pos:Array = item.position.split(" ");			
				elements[i].x = _convertX(pos[0]);
				elements[i].y = _convertY(pos[2]);
				Item(elements[i]).life = item.life;
				Item(elements[i]).frequence = item.tiroFrequence;
				Item(elements[i]).dist = item.distTargetAtack; 
				Item(elements[i]).speed = item.speed; 
				
				var points:Array = item.points.split("\n");			
				
				var p:Array;
				for each(var point:String in points)
				{
					p = point.split(" ");
					elements[i].points.push(new Vector3(_convertX(p[0]), _convertY(p[2])));
				}
				//elements[i].x = _convertX(pos[0]);
				//elements[i].y = _convertY(pos[2]);
				
				
				
				i++;
			}//end for
		}//end of method
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		private function _loadRoom():void
		{
			var size:Number = this.xmlData..rooms.Item.length();

			if (size <= 0)
				return;
			
			var i:uint = elements.length;
			for each (var room:XML in xmlData..rooms.Item)
			{
				
				var posMin:Array = room.posMin.split(" ");			
				var posMax:Array = room.posMax.split(" ");			
				
				posMin[0] = _convertX(posMin[0]);
				posMin[2] = _convertY(posMin[2]);
				
				posMax[0] = _convertX(posMax[0]);
				posMax[2] = _convertY(posMax[2]);
				
				elements[i] = new Item(room.name, Item.ROOM, new ShapePolygon(posMax[0]-posMin[0], posMax[2]-posMin[2],0xCFCFFF,0xCCCCFF,0.5));
				elements[i].x = posMin[0];
				elements[i].y = posMin[2];
				
				i++;
			}//end for			
		}//end of method
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		private function _loadPortal():void
		{
			var size:Number = this.xmlData..portals.Item.length();

			if (size <= 0)
				return;
			
			var i:uint = elements.length;
			for each (var item:XML in xmlData..portals.Item)
			{
				
				elements[i] = new Item(item.name, Item.PORTAL, new ShapeImage("img/portal.png"));
				
				var pos:Array = item.position.split(" ");			
				elements[i].x = _convertX(pos[0]);
				elements[i].y = _convertY(pos[2]);
				
				i++;
			}//end for
		}//end of method
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		private function _loadPowerUp():void
		{
			var size:Number = this.xmlData..powerups.Item.length();

			if (size <= 0)
				return;
			
			var i:uint = elements.length;
			for each (var item:XML in xmlData..powerups.Item)
			{
				if(item.name=="Life")
					elements[i] = new Item(item.name, Item.POWERUP, new ShapeImage("img/powerup_life.png"));
				else
					elements[i] = new Item(item.name, Item.POWERUP, new ShapeImage("img/powerup_shot.png"));
				
				var pos:Array = item.position.split(" ");			
				elements[i].x = _convertX(pos[0]);
				elements[i].y = _convertY(pos[2]);
				Item(elements[i]).powerup_life = item.life;
				Item(elements[i]).powerup_tiro = item.power;
				
				i++;
			}//end for
		}//end of method
		//~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
		
		
		private function _convertX(x:Number):Number
		{
			return (elements[0].x + x/10);
		}
		private function _convertY(y:Number):Number
		{
			return (elements[0].y + y/10);
		}
		
	}

}